package de.upb.aquarium.api.objects;

import java.util.ArrayList;
import java.util.List;

import javax.media.opengl.GL;

import de.upb.aquarium.World;


/**
 * A container that contains General3DObjects.
 * The coordinate system of the contained objects ist relative to the position of this class.
 * @author Alexander
 */
public class ComposedObject extends General3DObject {

	private List<IPaintable> containedObjects = new ArrayList<IPaintable>();
	float[] absolutePosition;


	@Override
	public void paint(GL gl) {
		beforeRendering(gl);

		for (IPaintable o : containedObjects) {
			if (o instanceof ComposedObject)
				((ComposedObject)o).setAbsolutePosition(absolutePosition);
			if (o instanceof TransparentObject) {// don't paint transparent objects here
				((TransparentObject)o).setAbsolutePosition(absolutePosition);
				World.getInstance().getTransparentObjects().add((TransparentObject)o);
			}
			else
				o.paint(gl);
		}

		afterRendering(gl);
	}

	public void add(IPaintable o) {
		containedObjects.add(o);
	}

	public List<IPaintable> getContainedObjects() {
		return containedObjects;
	}

	public void setContainedObjects(List<IPaintable> objects) {
		this.containedObjects = objects;
	}

	public void setAbsolutePosition(float[] absolutePosition) {
		float[] position = getPosition();
		this.absolutePosition = new float[] {
				absolutePosition[0] + position[0],
				absolutePosition[1] + position[1],
				absolutePosition[2] + position[2]
		};
	}
}
